//
//  WelcomeViewController.swift
//  WeiBoSwift
//
//  Created by rong on 15/6/28.
//  Copyright © 2015年 rong. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(backImageView)
        
        view.addSubview(iconView)
        view.addSubview(messageLable)
        
        // 设置头像
        if let urlString = sharedUserAccount?.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: urlString)!)
        }
        
        //控件布局
        setAutoLayout()
    }
    
    //iconview的距离底部的layout
    var iconvBottomConstraint: NSLayoutConstraint?
    
    //设置自动布局
    func setAutoLayout()
    {
        //背景
        backImageView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView" : backImageView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView" : backImageView]))
        
        //头像布局
        iconView.translatesAutoresizingMaskIntoConstraints = false

        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
    
        iconvBottomConstraint = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160)
        view.addConstraint(iconvBottomConstraint!)
        
        //原图大小是170/2 ＝ 85， 现在设置成90，在把layer 切成圆形的时候可以用45
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(90)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView" : iconView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(90)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView" : iconView]))
        
        
        //文字布局
        messageLable.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 8.0))
        
        
    }
    
    //界面出现后执行动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconvBottomConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 200)
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { (_) -> Void in
            
            //iconvBottomConstraint?.constant只是修改约束，真正实现的时候是 layoutIfNeeded() 执行的时候
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                //self.messageLable.alpha = 1.0
                
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    //self.messageLable.hidden = false
                    self.messageLable.alpha = 1.0
                })
                
                
                //欢迎界面出现一段时间后，切换界面，发送消息
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.messageLable.alpha = 1.0
                    }, completion: { (_) -> Void in
                        
                        //发送true , 是 MainViewController
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotication, object: true)
                })
        }
    }
    
    //背景imageView
    lazy var backImageView: UIImageView = {
        
        var bgimagev = UIImageView(image: UIImage(named: "ad_background"))
        
        return bgimagev
    }()
    
    //头像
    lazy var iconView: UIImageView = {
        
        var iconV = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconV.layer.masksToBounds = true
        iconV.layer.cornerRadius = 45
        
        return iconV
    }()
    
    //文字
    lazy var messageLable: UILabel = {
        
        var label = UILabel()
        label.text = "欢迎回来"
        label.sizeToFit()
        label.font = UIFont.systemFontOfSize(14.0)
        //label.hidden = true
        label.alpha = 0
        
        
        //设置label 没有边框
        return label
    }()

}
